package cm.mefobe.android.telecomcost.adapter;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

import cm.mefobe.android.telecomcost.TelecomCost;
import cm.mefobe.android.telecomcost.activities.VoiceRateListActivity;
import cm.mefobe.android.telecomcost.entities.VoiceRate;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class VoiceRateAdapter extends ArrayAdapter<VoiceRate> {
	private ArrayList<VoiceRate> items;
	private Context _context;

	public VoiceRateAdapter(Context context, int textViewResourceId,
			ArrayList<VoiceRate> items) {
		super(context, textViewResourceId, items);
		this.items = items;
		_context = context;
	}

	/**
	 * @param context
	 * @param resource
	 * @param textViewResourceId
	 * @param objects
	 */
	public VoiceRateAdapter(Context context, int resource,
			int textViewResourceId, ArrayList<VoiceRate> objects) {
		super(context, resource, textViewResourceId, objects);
		this.items = objects;
		_context = context;
	}

	/**
	 * @param context
	 * @param resource
	 * @param textViewResourceId
	 */
	public VoiceRateAdapter(Context context, int resource,
			int textViewResourceId) {
		super(context, resource, textViewResourceId);
		_context = context;
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public VoiceRateAdapter(Context context, int textViewResourceId,
			List<VoiceRate> objects) {
		super(context, textViewResourceId, objects);
		this.items = (ArrayList<VoiceRate>) objects;
		_context = context;
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public VoiceRateAdapter(Context context, int textViewResourceId,
			VoiceRate[] objects) {
		super(context, textViewResourceId, objects);
		_context = context;
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 */
	public VoiceRateAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		_context = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.i("VoiceRateAdapter", "getting VoiceRate view");
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) _context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(
					cm.mefobe.android.telecomcost.R.layout.voice_rate_view,
					null);
		}

		final VoiceRate VoiceRate = items.get(position);
		if (VoiceRate != null) {
			TextView txtDestination = (TextView) v
					.findViewById(cm.mefobe.android.telecomcost.R.id.txtDestination);
			if (txtDestination != null) {
				txtDestination.setText("Destination : "
						+ VoiceRate.getDestinationLabel());
			}

			TextView txtDates = (TextView) v
					.findViewById(cm.mefobe.android.telecomcost.R.id.txtDates);
			if (txtDates != null) {
				txtDates.setText(VoiceRate.getDatesLabel());
			}

			TextView txtCharges = (TextView) v
					.findViewById(cm.mefobe.android.telecomcost.R.id.txtCharges);
			if (txtCharges != null) {
				txtCharges.setText(VoiceRate.getChargesLabel());
			}
		}

		v.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				showEditDialog(VoiceRate);
			}
		});

		return v;
	}

	protected void showEditDialog(VoiceRate voiceRate) {
		Bundle data = new Bundle();
		Gson gson = TelecomCost.getGson();
		data.putString(
				cm.mefobe.android.telecomcost.entities.VoiceRate.PROP_TYPE,
				gson.toJson(voiceRate));
		((Activity) _context).showDialog(
				VoiceRateListActivity.DIALOG_VOICE_RATE, data);
	}

	protected void selectVoiceRate(VoiceRate VoiceRate, boolean arg1) {
		VoiceRate.setSelected(arg1);

		if (arg1) {
			Log.i("VoiceRate_ListView", "zone " + VoiceRate.getName()
					+ " checked");
		} else {
			Log.i("VoiceRate_ListView", "zone " + VoiceRate.getName()
					+ " unchecked");
		}
	}
}
